package com.ano.service;

import com.ano.dao.ClusterDAO;
import com.ano.entity.Cluster;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class ClusterServiceImpl implements ClusterService{

    @Autowired
    ClusterDAO clusterDAO;

    /**
     * 分页查询所有
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Cluster> findAllForPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Cluster> clusterList = clusterDAO.findAll();
        if(clusterList!=null)
        {
            PageInfo<Cluster> res = new PageInfo<Cluster>(clusterList);
            return res;
        }else {
            throw new RuntimeException("查找失败！！！");
        }
    }

    /**
     * 通过名字分页查询
     * @param pageNum
     * @param pageSize
     * @param clusterName
     * @return
     */
    @Override
    public PageInfo<Cluster> findByNameForPage(Integer pageNum, Integer pageSize, String clusterName) {
        PageHelper.startPage(pageNum, pageSize);
        //替换为正则表达式达到模糊查询
        String oldName = clusterName;
        clusterName = "%"+clusterName+"%";
        List<Cluster> clusterList = clusterDAO.findByName(clusterName);
        if(!clusterList.isEmpty())
        {
            PageInfo<Cluster> res = new PageInfo<Cluster>(clusterList);
            return res;
        }else {
            throw new RuntimeException("未找到与"+oldName+"相关的簇");
        }
    }

    /**
     * 通过类型分页查询
     * @param pageNum
     * @param pageSize
     * @param typeName
     * @return
     */
    @Override
    public PageInfo<Cluster> findByTypeForPage(Integer pageNum, Integer pageSize, String typeName) {
        PageHelper.startPage(pageNum, pageSize);
        List<Cluster> clusterList = clusterDAO.findByType(typeName);
        if(!clusterList.isEmpty())
        {
            PageInfo<Cluster> res = new PageInfo<Cluster>(clusterList);
            return res;
        }else {
            throw new RuntimeException("该类型还没有簇！！！");
        }
    }

    /**
     * 通过类型和名字
     * @param pageNum
     * @param pageSize
     * @param clusterName
     * @param typeName
     * @return
     */
    @Override
    public PageInfo<Cluster> findByNameAndTypeForPage(Integer pageNum, Integer pageSize, String clusterName, String typeName) {
        PageHelper.startPage(pageNum, pageSize);
        //替换为正则表达式达到模糊查询
        clusterName = "%"+clusterName+"%";
        List<Cluster> clusterList = clusterDAO.findByNameAndType(clusterName,typeName);
        if(!clusterList.isEmpty())
        {
            PageInfo<Cluster> res = new PageInfo<Cluster>(clusterList);
            return res;
        }else {
            throw new RuntimeException("查找失败！！！");
        }
    }

    /**
     * 通过簇名字精确查询
     * @param clustername
     * @return
     */
    @Override
    public Cluster findByFullName(String clustername) {
        Cluster cluster = clusterDAO.findByFullName(clustername);
        //如果为空说明没找到
        if(cluster==null){
            throw new RuntimeException("不存在名字为"+clustername+"的簇！！！");
        }
        return cluster;
    }

    /**
     * 通过簇id查询
     * @param id
     * @return
     */
    @Override
    public Cluster findById(String id) {
        Cluster cluster = clusterDAO.findById(id);
        //如果为空说明没找到
        if(cluster==null){
            throw new RuntimeException("不存在id为"+id+"的簇！！！");
        }
        return cluster;
    }

    /**
     * 不分页的查询全部
     * @return
     */
    @Override
    public List<Cluster> findAll() {
        List<Cluster> clusters = clusterDAO.findAll();
        if(clusters.isEmpty()){
            throw new RuntimeException("查找全部失败！！！");
        }
        return clusters;
    }

    /**
     * 这里不管查没查到就都返回
     * @param hostid
     * @return
     */
    @Override
    public List<Cluster> findByHostId(String hostid) {
        return clusterDAO.findByHostId(hostid);
    }


    /**
     * 添加簇
     * @param cluster
     */
    @Override
    public void add(Cluster cluster,List<String> typeNames) {
        //判断是否重名
        Cluster findCluster = clusterDAO.findByFullName(cluster.getClustername());
        if(findCluster!=null){
            throw new RuntimeException("簇重名了！！！");
        }
        //不重名
        //生成时间和id存入
        Date createTime = new Date();
        cluster.setCreatetime(createTime);
        String id = UUID.randomUUID().toString();
        cluster.setId(id);
        //将cluster存入数据库
        clusterDAO.save(cluster);
        for (String typeName : typeNames) {
            //存储cluster与type多对多关系
            clusterDAO.saveType(id,typeName);
        }
    }

    /**
     * 删除簇
     * 流程：先删除t_clus_type中簇所对应
     * 再删除t_cluster里面的簇
     * @param id
     */
    @Override
    public void delete(String id) {
        clusterDAO.deleteType(id);
        clusterDAO.delete(id);
    }

    /**
     * 修改簇
     * 先把t_clust_type删除
     * 再加入新的
     * @param cluster
     */
    @Override
    public void update(Cluster cluster,List<String> typeNames) {
        clusterDAO.update(cluster);
        clusterDAO.deleteType(cluster.getId());
        for (String typeName : typeNames) {
            clusterDAO.saveType(cluster.getId(),typeName);
        }
    }

    /**
     * 通过簇id查找出所拥有的type
     * @param clusid
     * @return
     */
    @Override
    public List<String> findTypesByClusId(String clusid) {
        return clusterDAO.findTypesByClusid(clusid);
    }
}
